


(function(){
	var BarView = G.GClass('Widget', 'BarView', G.Base.EventListener);

	BarView.prototype.__init = function(groups) 
	{
		this.__Base__EventListener__init();
		this._groups = groups; 
		this._blkList = [];
	}

	BarView.prototype.render = function(container){
		this._emContainer = container;
		for (var i = 0; i < this._groups.length; i++) {
			this._draw_group(this._groups[i]);
		}
	}

	BarView.prototype._draw_group = function(grp) 
	{
		var title = grp.title || grp.name;

		var box = document.createElement('div'); 
		var header = document.createElement('div');
		var body = document.createElement('div');

		box.className = 'barview_block';
		header.innerHTML = title;
		header.name = grp.name;
		header.className = 'barview_head';
		body.className = 'barview_body';

		box.appendChild(header);
		box.appendChild(body);
		this._emContainer.appendChild(box);

		var blk = {
			bar: G(header),
			body: body,
			title: title,
			unfolded: true 
		}
		this._blkList[grp.name] = blk;
	}


	BarView.prototype.get_body = function(name)
	{
		if (name in this._blkList) {
			return this._blkList[name].body;
		}
		return null;
	}


	BarView.prototype._on_bar_click = function(evt)
	{
		var bar = evt.srcElement;
		var blk = this._blkList[bar.name];
		if (blk.unfolded) {
			blk.unfolded = false;
			bar.parentNode.className = "barview_block foldered";

		} else {
			blk.unfolded = true;
			bar.parentNode.className = "barview_block";
		}
	}


	G.DOM.Element.prototype.BarView = function(groups) 
	{
		var bv = new G.Widget.BarView(groups);
		bv.render(this.em);
		return bv;
	}
	
 })();
